package com.example.myproject.util.deptlevel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.myproject.util.FileUtil;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DeptUtil {
    //1层1个空格 2层4 *（level-1）=5  //3层 = 4 *(n-1) = 5
    static Map<Integer,JSONObject> LevelMap = new HashMap<>();


    public static void main(String[] args) {
        String sa ="moduelMarks参数设置moduelMarks";
        String[] f2  = sa.split("moduelMarks");
        System.out.println(f2);


        List<String> conetent = FileUtil.readTxt(new File("e:/dept.txt"));

        StringBuilder sb = new StringBuilder();
        for (String s : conetent) {
            sb.append(s);
        }
        JSONArray array = JSON.parseArray(sb.toString());
        for (int i = 0; i < array.size(); i++) {
            JSONObject object = (JSONObject) array.get(i);
            String departname = (String) object.get("DEPTNAME");
            String[] a2 = departname.split("&nbsp;");
            int nbspnum = a2.length - 1;
            //根据nbspnum计算层级
            int level = (nbspnum + 3 )/ 4;

           // System.out.println("deptname:"+departname+",level:"+level);

            if(1 == level){
                LevelMap.put(level,object);
            }else{
                LevelMap.put(level,object);
                object.put("PID",LevelMap.get(level-1).get("ID"));
                object.put("PDEPTNAME",LevelMap.get(level-1).get("DEPTNAME"));
            }
            object.put("level",level);
            if (a2.length - 1 == 29) {
               // System.out.println("length:" + (a2.length - 1));
               // System.out.println("departname:" + departname);
            }
        }

        System.out.println(array.toString());


    }
}
